package com.cby.user.test3D;

import lombok.Data;

import java.util.Arrays;
import java.util.Random;

/*
**  Create by: 王孙康宏
    Date:2021-10-24
    Time:22:33
*/
@Data
public class UpdateUtil {
    /**
     * 传入一个实例，将其中的box列表进行打乱，并初始化实例参数，返回一个全新的实例
     */
    public Instance updateBoxes(Instance oldInstance){
        Box[] boxes = oldInstance.getBoxes();
        // 对每个箱子的可行尺寸 进行打乱操作 两两互换
        for (int i = 0; i < boxes.length; i++) {
            float[][] orientations = boxes[i].getOrientations();
            for (int j = 0; j < 20; j++) {
                int r1 = new Random().nextInt(orientations.length);
                int r2 = new Random().nextInt(orientations.length);
                float[] temp = orientations[r1].clone();
                orientations[r1] = orientations[r2].clone();
                orientations[r2] = temp.clone();
            }
            boxes[i].setOrientations(orientations.clone());
        }
        Instance newInstance = new Instance(oldInstance);
        Arrays.sort(boxes);
        newInstance.setBoxes(boxes.clone());
        return newInstance;
    }
}
